package com.mrogrape.notepad;

import java.awt.EventQueue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import com.mrogrape.notepad.activity.MainFrame;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.resource.Settings;
import com.mrogrape.notepad.ui.editor.ContentType;
import com.mrogrape.notepad.ui.editor.TextEditor;

public class Notepad {
	
	private static final String VERSION = "0.9.1";

	/**
	 * 退出程序
	 * 
	 * @param status
	 */
	public static void exit(int status) {

		try {
			Settings.saveSettings();
		} catch (FileNotFoundException e1) {
			System.exit(1);
		} catch (IOException e1) {
			System.exit(1);
		}
		System.exit(0);
	}

	/**
	 * @param args
	 * @throws InvocationTargetException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, InvocationTargetException {

		// 加载用户配置
		try {
			Settings.loadSettings();
		} catch (Exception e) {
			e.printStackTrace();
			Settings.resetAllSettings();
		}

		// 加载主界面
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {

				MainFrame frame = ViewContext.getMainFrame();
				frame.showFrame();
				TextEditor editor = frame.createNewEditor(ContentType.PLAIN_TEXT);
				editor.requestFocus();
				if(Settings.getSettings().getViewSettings().getShowLibraryView()){
					ViewContext.getLibraryPane().buildStructure();
				}
				
				if(!Notepad.VERSION.equals(Settings.getSettings().version)){
					Settings.getSettings().version = Notepad.VERSION;
					ViewContext.getUpdateInfoDialog().setVisible(true);
				}
			}
		});
	}

}
